package example.JAXB.JAXBexample;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class UnmarshallerSample {
	public String getObjectFromJAXB() throws JAXBException{
		String jaxbString = "";
		// =============================================================================================================
        // Setup JAXB
        // =============================================================================================================
 
        // Create a JAXB context passing in the class of the object we want to marshal/unmarshal
        final JAXBContext context = JAXBContext.newInstance(SampleObject.class);
		
		// =============================================================================================================
        // Unmarshalling XML to OBJECT
        // =============================================================================================================
 
        // Create the unmarshaller, this is the nifty little thing that will actually transform the XML back into an object
        final Unmarshaller unmarshaller = context.createUnmarshaller();
 
        // Unmarshal the XML in the stringWriter back into an object
        final SampleObject sampleObject = (SampleObject) unmarshaller.unmarshal(new StringReader(jaxbString));
 
        // Print out the contents of the JavaObject we just unmarshalled from the XML
        System.out.println(sampleObject.toString());
        
		return jaxbString;
	}
}
